﻿using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.GERP.GHRMS.DAL;
using System;
using System.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class ExportReport : System.Web.UI.Page
    {
        MailMerge mailMerge = new MailMerge();
        Main main = new Main();
        protected void Page_Load(object sender, EventArgs e)
        {
            string strReportName = Request.QueryString["report"];
            LoadReport(strReportName);
        }

        private void LoadReport(string strReportName)
        {
            switch (strReportName)
            {
                case "SalarySlip" :
                    LoadSalarySlip();
                    break;
            }
        }

        private void LoadSalarySlip()
        {
            int nEmpSeq = new SsMng().EmpSeqI;
            int? nYear = main.ConvertNullInt(Request.QueryString["year"]);
            int? nMonth = main.ConvertNullInt(Request.QueryString["month"]);

            if (!nYear.HasValue || !nMonth.HasValue)
            {
                return;
            }

            PayrollDAL payroll = new PayrollDAL();
            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("~/Reports/SalarySlip.rpt"));
            rpt.SetParameterValue(0, "asdasd");

            var dtb = payroll.GetReport(nYear.Value, nMonth.Value, nEmpSeq);
            if (dtb != null && dtb.Rows.Count > 0)
            {
                if (!string.IsNullOrEmpty(dtb.Rows[0]["Net"].ToString()))
                {
                    dtb.Rows[0]["NetThaiText"] = main.MoneyTextThai(dtb.Rows[0]["Net"].ToString());
                }

                dtb.Rows[0]["BankAccountNo"] = payroll.GetBankAccountNo(nEmpSeq);
                rpt.SetDataSource(dtb);

                rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "SalarySlip");
            }
            else
            {
                main.MessageAlert(Page, "ไม่พบข้อมูลเงินเดือนของเดือนนี้ " + Request.QueryString["month"] + "/" + Request.QueryString["year"]);
            }
        }
    }
}